package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.Util;
import com.glowdayz.glowmee.cmd.PouchCommentCMD;
import com.glowdayz.glowmee.dao.PouchCommentDAO;

public class PouchCommentBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public PouchCommentBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		String where = request.getParameter("where");
		if (where != null) {
			where = Util.getInstance().incoding(where);
		} else {
			where = "";
		}
		
		String nickName = request.getParameter("nickName");
		String commentText = request.getParameter("commentText");
		String start_date = request.getParameter("start_date");
		String end_date = request.getParameter("end_date");
		if (nickName != null && !nickName.equals("")) {
			nickName = Util.getInstance().incoding(nickName);
			where += " AND r.nickName like '%" + nickName + "%'";
		}
		if (commentText != null && !commentText.equals("")) {
			commentText = Util.getInstance().incoding(commentText);
			where += " AND p.pouchComment like '%" + commentText + "%'";
		}
		if (start_date != null && !start_date.equals("") && end_date != null && !end_date.equals("")) {
			start_date = start_date.replace("-", "");
			start_date += "000000";
			end_date += "999999";
			end_date = end_date.replace("-", "");
			where += " AND p.create_date BETWEEN " + start_date + " AND " + end_date;
		}
		
		String id = request.getParameter("id");
		
		String idPouchComment_str = request.getParameter("idPouchComment");
		String isDisplay_str = request.getParameter("isDisplay");
		if (idPouchComment_str != null && isDisplay_str != null) {
			int idPouchComment = Integer.parseInt(idPouchComment_str);
			int isDisplay = Integer.parseInt(isDisplay_str);
			if (isDisplay == 1) {
				isDisplay = 0;
			} else if (isDisplay == 0) {
				isDisplay = 1;
			}
			try {
				PouchCommentDAO.getInstance().isDisplayComment(new PouchCommentCMD(idPouchComment, isDisplay));
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		try {
			if (!where.equals("")) request.setAttribute("where", "&where=" + URLEncoder.encode(where, "UTF-8"));
			request.setAttribute("id", id);
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("comment", PouchCommentDAO.getInstance().getCommentList(1, -1, "p.idPouch=" + id + where));
			request.setAttribute("display", DBHandler.getInstance().getCount(
					"pouchcomment p, register r", "*", "p.idRegister=r.idRegister AND p.idPouch=" + id + " AND p.isDisplay='1'" + where));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount(
					"pouchcomment p, register r", "*", "p.idRegister=r.idRegister AND p.idPouch=" + id + " AND p.isDisplay='0'" + where));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/pouchComment.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}